<?php

class Campaign extends Controller {
    function Campaign() {
        parent::Controller();
        $this->load->model('model_campaign');
		$this->load->model('model_data');	
		

        if (!$this->usersession->userLoggedIn()) {
            $aResponse = array("error" => true, "description" => "Sesiunea a expirat", "type" => "nosession");
            die(json_encode($aResponse));
        }
        $this->fromSession = $this->usersession->currentUser();
    }

    /**
     * Functie care aduce toate contactele de pe campanie ( baza de date , externe)
     */
    function bringCampaignContact() {
        $s_limit = $this->input->post("start");
        $e_limit = $this->input->post("limit");
        $dir = $this->input->post("dir");
        $sort = $this->input->post("sort");
        switch ($sort) {
            case 'client_name':
                $sort = "client_name";
                break;
            case 'client_name':
                $sort = "client_name";
                break;
        }

        echo json_encode($this->model_campaign->bringCampaignContact($s_limit, $e_limit, $sort, $dir, $_POST));
    }

    function bringCampaign() {
        $table = 'client';
        $s_limit = $this->input->post("start");
        $e_limit = $this->input->post("limit");
        $dir = $this->input->post("dir");
        $sort = $this->input->post("sort");
        switch ($sort) {
            case 'name':
                $sort = "$table.name";
                break;
            default :
                $sort = "$table.name";
                break;
        }
        echo json_encode($this->model_campaign->bringCampaign($table, $s_limit, $e_limit, $sort, $dir, $_POST));
    }

    function deleteCampaign() {
        if ($this->fromSession['level'] != "admin") {
            $aResponse = array("error" => true, "description" => "Nu aveti drept de stergere campanie.. :P", "type" => "security");
            echo json_encode($aResponse);
            $aResponse = array("error" => true, "description" => "Nu aveti drept de stergere campanie.. :P", "type" => "security");
            echo json_encode($aResponse);
            return;
        }
        $result = $this->model_campaign->deleteCampaign($_POST);
        $aResponse = array("error" => false, "description" => "Campania a fost stearsa.");
        echo json_encode($aResponse);
    }

    /**
     * Operatii de atasare /scoatere contactului de pe o campanie
     */
    function opContact() {
        $result = $this->model_campaign->opContact($_POST);
        $aResponse = array("error" => false, "description" => "Datele au fost salvate", "success" => true);
        echo json_encode($aResponse);
    }

    /**
     * Functie care dezaboneaza / aboneaza sau sterge de pe o campanie
     */
    function opCampaignContact() {
        $result = $this->model_campaign->opCampaignContact($_POST);
        $aResponse = array("error" => false, "description" => "Operatia a fost efectuata", "type" => "data");
        if (!$result) {
            $aResponse = array("error" => true, "description" => "Operatia NU a fost efetuata! ", "type" => "data");
        }
        echo json_encode($aResponse);
    }

    /**
     * Functie de dadaugare contact extern
     */
    function addContact() {
        $result = $this->model_campaign->addContact($_POST);
        $aResponse = array("error" => false, "description" => "Operatia a fost efectuata", "type" => "data", "success" => true);
        if (!$result) {
            $aResponse = array("error" => true, "description" => "Operatia NU a fost efetuata! ", "type" => "data");
        }
        echo json_encode($aResponse);
    }

    function getContact() {
        $result = $this->model_campaign->getContact($_POST);
        echo ('{"error":false,"success":true,"data":' . json_encode($result) . "}");
    }

    /**
     * Functie de dadaugare contact extern
     */
    function editContact() {
        $result = $this->model_campaign->editContact($_POST);
        $aResponse = array("error" => false, "description" => "Operatia a fost efectuata", "type" => "data", "success" => true);
        if (!$result) {
            $aResponse = array("error" => true, "description" => "Operatia NU a fost efetuata! ", "type" => "data");
        }
        echo json_encode($aResponse);
    }
				
	function export() {
		$aPost =  json_decode($this->input->post("params"), true);			
		
        //creare array cu useri
        $data["table"] = "user";
        $aUsersFromTable = $this->model_data->bringObject($data);
        $aUsers = array();
        foreach ($aUsersFromTable as $user) {
            $aUsers[$user["id"]] = $user["name"];
        }


        $customs["age"] = $this->config->item("ageContact");
        $customs["income"] = $this->config->item("incomeContact");
        $customs["employees"] = $this->config->item("employeeContact");
        $customs["available"] = $this->config->item("availableContact");
        $customs["priority"] = $this->config->item("priorityContact");
        $customs["sex"] = $this->config->item("sexContact");
		
		$aData = $this->model_campaign->getCampaignData($aPost);
		
		   header("Content-type: application/vnd.ms-excel;charset=utf-8");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-disposition:  attachment; filename=raport.xls ");
		
		echo "<table>";
		echo "<tr>
						<td width=150>Companie</td>
						<td width=150>Nume</td>
						<td width=150>Prenume</td>
						<td width=150>Titlu</td>
						<td width=150>Domeniu</td>
						<td width=150>Subdomeniu</td>
						<td width=150>Functie</td>
						<td width=150>Subfunctie</td>
						<td width=150>Departament</td>
						<td width=150>Adresa</td>
						<td width=150>Cod Postal</td>
						<td width=150>Oras</td>
						<td width=150>Judet</td>
						<td width=150>Telefon</td>
						<td width=150>Mobil</td>
						<td width=150>Email</td>
						<td width=150>Email2</td>
						<td width=150>Adresa Web</td>						
						<td width=150>Zi nastere</td>
						<td width=150>Campanie</td>
						<td width=150>Grupa Varsta</td>
						<td width=150>Grupa Venit</td>
						<td width=150>Identificat</td>
						<td width=150>Prioritate</td>
						<td width=150>Cifra Afaceri 2009</td>
						<td width=150>Nr Angajati</td>
						<td width=150>Observatii</td>
						<td width=150>Observatii Companie</td>
						<td width=150>Cod Inregistrare</td>
						<td width=150>Cifra Afaceri 2010</td>
						<td width=150>Cifra Afaceri 2011</td>
						<td width=150>Ultima modificare</td>
						<td width=150>Data ultima modificare</td>
						<td width=150>Penultima modificare</td>
						<td width=150>Data penultima modificare</td>
						<td width=150>Creat de</td>
						<td width=150>Data creare</td>
						<td width=150>Dezabonat</td>
						<td width=150>ID Baza de date</td>							
				 </tr>";	 
				 		
	foreach ($aData as $key => $row) {
		
	
	echo 
	   "<tr><td>" . strUpper( $row["company"]) . "</td>".
	    "<td>" . strTitle($row["lname"]) . "</td>".
	    "<td>" . strTitle($row["fname"]) . "</td>".
	    "<td>" . $customs["sex"][$row["sex"]] . "</td>".
	    "<td>" . $row["domain_name"] . "</td>".
	    "<td>" .$row["subdomain_name"] . "</td>".
	    "<td>" .  $row["position_name"] . "</td>".
	    "<td>" . $row["subposition_name"] . "</td>".
	    "<td>" . strTitle($row["division_name"]) . "</td>".
	    "<td>" . $row["address"] . "</td>".
	    "<td>" . $row["address2"] . "</td>".
	    "<td>" . strTitle( $row["city_name"]) . "</td>".
	    "<td>" . strTitle($row["county_name"]) . "</td>".
	    "<td>" .  $row["phone"] . "</td>".
	    "<td>" . $row["mobil"] . "</td>".
	    "<td>" . $row["email"] . "</td>".
	    "<td>" . $row["email2"] . "</td>".
	    "<td>" .  $row["web"] . "</td>".
	    "<td>" . $row["birth"] . "</td>".
	    "<td>" . $row["campanie"] . "</td>".
	    "<td>" . $customs["age"][$row["age"]] . "</td>".
	    "<td>" . $customs["income"][$row["income"]] . "</td>".
	    "<td>" . $customs["available"][$row["available"]] . "</td>".
	    "<td>" . $customs["priority"][$row["priority"]] . "</td>".
	    "<td>" . $row["turnover"] . "</td>".
	    "<td>" . $customs["employees"][$row["employees"]] . "</td>".
	    "<td>" . $row["obs"] . "</td>".
	    "<td>" . $row["obs_company"] . "</td>".
	    "<td>" . $row["cui"] . "</td>".
	    "<td>" . $row["turnover2010"] . "</td>".
	    "<td>" .$row["turnover2011"] . "</td>".
	    "<td>" . $aUsers[$row["modify_by"]] . "</td>".
	    "<td>" . substr($row["date_modified"], 0, 10) . "</td>".
	    "<td>" . $aUsers[$row["modify_by2"]] . "</td>".
	    "<td>" . substr($row["date_modified2"], 0, 10) . "</td>".
	    "<td>" . $aUsers[$row["create_by"]] . "</td>".
	    "<td>" . substr($row["date_created"], 0, 10) . "</td>".
	    "<td>" . $customs["available"][$row["unsubscribed_campanie"]] . "</td>".
	    "<td>" . $row["person_id"] . "</td></tr>";
	    }
		echo "</table>";
		
											
	}
}
